import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ManagerPublishEventFrame extends JFrame {
    private JTextField idField = new JTextField(15);
    private JTextField nameField = new JTextField(15);
    private JTextArea rulesArea = new JTextArea(5, 15);
    private JTextField startTimeField = new JTextField(15);
    private JTextField endTimeField = new JTextField(15);
    private JTextField locationField = new JTextField(15);
    private RoundedButton publishButton = new RoundedButton("发布项目");

    private EventManager eventManager = new EventManager();
    private Manager manager = new Manager("M001", "管理员");

    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public ManagerPublishEventFrame() {
        setTitle("河南工业大学运动会项目发布");
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBackground(new Color(255, 245, 250)); // 粉白色背景

        // 顶部标题
        JLabel titleLabel = new JLabel("河南工业大学运动会管理系统", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(new Color(255, 105, 180));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        // 表单区
        JPanel formPanel = new JPanel(new GridBagLayout());
        formPanel.setOpaque(false);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.CENTER;

        int row = 0;
        addCenteredRow(formPanel, gbc, row++, "项 目 I D:", idField);
        addCenteredRow(formPanel, gbc, row++, "项目名称:", nameField);
        addCenteredRow(formPanel, gbc, row++, "项目规则:", new JScrollPane(rulesArea));
        addCenteredRow(formPanel, gbc, row++, "开始时间:", startTimeField);
        addCenteredRow(formPanel, gbc, row++, "结束时间:", endTimeField);
        addCenteredRow(formPanel, gbc, row++, "地      点:", locationField);

        mainPanel.add(formPanel, BorderLayout.CENTER);

        // 发布按钮
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setOpaque(false);
        publishButton.setPreferredSize(new Dimension(160, 45));
        publishButton.setBackground(new Color(255, 182, 193));
        publishButton.setForeground(Color.WHITE);
        publishButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        publishButton.setFocusPainted(false);
        buttonPanel.add(publishButton);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        publishButton.addActionListener(e -> publishEvent());

        add(mainPanel);
    }

    private void addCenteredRow(JPanel panel, GridBagConstraints gbc, int row, String labelText, Component field) {
        JPanel rowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
        rowPanel.setOpaque(false);

        JLabel label = new JLabel(labelText);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        rowPanel.add(label);
        rowPanel.add(field);

        gbc.gridx = 0;
        gbc.gridy = row;
        gbc.gridwidth = 2;
        panel.add(rowPanel, gbc);
    }

    private void publishEvent() {
        try {
            String id = idField.getText().trim();
            String name = nameField.getText().trim();
            String rules = rulesArea.getText().trim();
            Date start = dateFormat.parse(startTimeField.getText().trim());
            Date end = dateFormat.parse(endTimeField.getText().trim());
            String location = locationField.getText().trim();

            if (id.isEmpty() || name.isEmpty() || rules.isEmpty() || location.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请填写完整信息！");
                return;
            }
            if (start.after(end)) {
                JOptionPane.showMessageDialog(this, "开始时间不能晚于结束时间！");
                return;
            }

            Event event = new Event(id, name, rules, start, end, location);
            boolean success = eventManager.addEvent(manager, event);

            if (success) {
                JOptionPane.showMessageDialog(this, "项目发布成功！");
                clearForm();
            } else {
                JOptionPane.showMessageDialog(this, "发布失败！");
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "时间格式错误，请使用 yyyy-MM-dd HH:mm");
        }
    }

    private void clearForm() {
        idField.setText("");
        nameField.setText("");
        rulesArea.setText("");
        startTimeField.setText("");
        endTimeField.setText("");
        locationField.setText("");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ManagerPublishEventFrame frame = new ManagerPublishEventFrame();
            frame.setVisible(true);
        });
    }

    // 圆角按钮类
    static class RoundedButton extends JButton {
        public RoundedButton(String text) {
            super(text);
            setContentAreaFilled(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            if (getModel().isPressed()) {
                g2.setColor(new Color(255, 105, 180));
            } else if (getModel().isRollover()) {
                g2.setColor(new Color(255, 192, 203));
            } else {
                g2.setColor(getBackground());
            }

            g2.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30);

            g2.setColor(getForeground());
            FontMetrics fm = g2.getFontMetrics();
            int x = (getWidth() - fm.stringWidth(getText())) / 2;
            int y = (getHeight() + fm.getAscent() - fm.getDescent()) / 2;
            g2.drawString(getText(), x, y);

            g2.dispose();
            super.paintComponent(g);
        }

        @Override
        protected void paintBorder(Graphics g) {
            // 不画边框
        }
    }

    // 模拟 EventManager
    static class EventManager {
        public boolean addEvent(Manager m, Event e) {
            System.out.println("已添加项目：" + e.name);
            return true;
        }
    }

    // 模拟 Manager
    static class Manager {
        public String id, name;
        public Manager(String id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    // 模拟 Event
    static class Event {
        public String id, name, rules, location;
        public Date start, end;
        public Event(String id, String name, String rules, Date start, Date end, String location) {
            this.id = id;
            this.name = name;
            this.rules = rules;
            this.start = start;
            this.end = end;
            this.location = location;
        }
    }
}
